/*
* Copyright 2010 tekhniaG authors
*
* Licensed under the LGPL, Version 3.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*      http://www.gnu.org/licenses/lgpl.html
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package tekhnia.com.tekhniag.objects.layouts {
	import mx.core.ILayoutElement;
	
	import spark.layouts.supportClasses.LayoutBase;
	
	/**
	 *
	 * defines the layout for the labels and graduations in a radio selector
	 * @author Victor Andritoiu
	 * 
	 */ 
	public class TekhniaCircularGaugeLayout extends LayoutBase {
		
		/**
		 * @private
		 *  
		 */ 
		private var _angularRange: Number = 360;
		/**
		 * angular range of the circular data group
		 * @default 360
		 * 
		 */
		public function get angularRange(): Number {
			return _angularRange;
		}
		public function set angularRange(value: Number): void {
			_angularRange = value;
			updateDisplayList(target.width, target.height);
		}
		
		/**
		 * @private
		 */
		private var _startAngle: Number = 0;
		/**
		 * start angle of the circular data group
		 * @default 0
		 * 
		 */
		public function get startAngle(): Number {
			return _startAngle;
		}
		public function set startAngle(value: Number): void {
			_startAngle = value;
			updateDisplayList(target.width, target.height);
		}
		
		/**
		 * @override
		 * overrides the updateDisplayList function in order to update
		 * layout
		 * 
		 */
		override public function updateDisplayList(w: Number, h: Number): void {
			super.updateDisplayList(w, h);
			
			if (!target)
				return;
			
			var layoutElement: ILayoutElement;
			var count: uint = target.numElements;
			
			var angle: Number = angularRange/(count-1);
			var radius: Number = Math.min( target.width/2, target.height/2 );
			
			var w2 : Number = target.width/2;
			var h2 : Number = target.height/2;
			
			for (var i:int = 0; i < count; i++) {
				layoutElement = target.getElementAt(i);
				
				if (!layoutElement || !layoutElement.includeInLayout)
					continue;
				
				var radAngle : Number = (angle * i) * (Math.PI / 180) + (startAngle-90) * (Math.PI / 180) ;
				
				var _x : Number = Math.sin( radAngle );
				var _y : Number = - Math.cos( radAngle );
				
				layoutElement.setLayoutBoundsPosition( w2 + (_x * radius)-15, h2 + (_y * radius)-15 );
			} 
		}
	}
}